{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "from moviepy.editor import * # imports everything, quick and dirty\n",
    "import librosa\n",
    "import time\n",
    "audio_path = '1.mp3'\n",
    "audio_origin_path = '.MP4'#\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "y, sr =  librosa.load(audio_path)\n",
    "tempo, beats = librosa.beat.beat_track(y=y, sr=sr)\n",
    "beats_sequence=librosa.frames_to_time(beats, sr=sr)\n",
    "beats_sequence_standard=beats_sequence-beats_sequence[0]\n",
    "print(len(beats_sequence))\n",
    "print(beats_sequence[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "diff_number=[2,2,2,2,2,3,2]\n",
    "wrong_position=[0,0,0,0,0,0,0]\n",
    "beats_number=[]\n",
    "time_move=[]\n",
    "beats_origin_sequence=[]\n",
    "for i in range(2,9):\n",
    "    i_path=str(i)+audio_origin_path\n",
    "    y, sr =  librosa.load(i_path)\n",
    "    tempo, beats = librosa.beat.beat_track(y=y, sr=sr)\n",
    "    beats_origin_sequence=librosa.frames_to_time(beats, sr=sr)\n",
    "    time_move.append(beats_sequence[0]-beats_origin_sequence[diff_number[i-2]-1])\n",
    "    \n",
    "    beats_number.append(len(beats_origin_sequence))   \n",
    "    beats_origin_sequence=beats_origin_sequence-beats_origin_sequence[diff_number[i-2]]\n",
    "    #print( beats_origin_sequence[0:10])\n",
    "    diff=[]\n",
    "    for j in range(0,250):       \n",
    "        diff.append(beats_origin_sequence[j+diff_number[i-2]]-beats_sequence_standard[j])\n",
    "        if(diff[j]>0.2):\n",
    "            wrong_position[i-2]=j\n",
    "            break;\n",
    "    \n",
    "# print(len(beats_origin_sequence))\n",
    "# print(beats_origin_sequence[diff_number:diff_number+10])\n",
    "# len(librosa.frames_to_time(beats, sr=sr))\n",
    "# print(beats_number)\n",
    "# print(wrong_position)\n",
    "print(time_move)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "time_move_base_on_fisrt_vedio=[]\n",
    "for x in range(0,len(time_move)):\n",
    "    if (x==0):\n",
    "        time_move_base_on_fisrt_vedio.append(0)\n",
    "    else:\n",
    "        time_move_base_on_fisrt_vedio.append(time_move[x]-time_move[x-1])\n",
    "print(time_move_base_on_fisrt_vedio)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "clip_time=[]\n",
    "vedio=[]\n",
    "vedio_clip=[]\n",
    "audio_duration = librosa.get_duration(filename=audio_path)\n",
    "video_number=6\n",
    "first_clip_index=0\n",
    "first_change_beats=15\n",
    "last_change_beats=3\n",
    "last_seed=0\n",
    "start_seed=1\n",
    "for i in range(2,2+video_number):\n",
    "    vedio.append(VideoFileClip(str(i)+audio_origin_path))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for t in range(10):   \n",
    "    video_out=0\n",
    "    vedio_clip=[]\n",
    "    for j in range(first_change_beats,len(beats_sequence)-last_change_beats):    \n",
    "        seed=random.randint(0,video_number-1)\n",
    "        if(seed==last_seed):\n",
    "            seed=(seed+1)%video_number\n",
    "        if(j==first_change_beats):\n",
    "            seed=start_seed\n",
    "            vedio_clip.append(vedio[seed].subclip(0,beats_sequence[j]-time_move[seed]))\n",
    "            first_clip_index=seed\n",
    "        else :\n",
    "            vedio_clip.append(vedio[seed].subclip(beats_sequence[j-1]-time_move[seed],beats_sequence[j]-time_move[seed]))\n",
    "        last_seed=seed\n",
    "    last_seed=0\n",
    "    vedio_clip.append(vedio[last_seed].subclip(\n",
    "    beats_sequence[len(beats_sequence)-last_change_beats-1]-time_move[last_seed],audio_duration -time_move[last_seed]))\n",
    "    video_out=concatenate_videoclips(vedio_clip)\n",
    "    audioclip = AudioFileClip(\"1.mp3\")\n",
    "    audioclip=audioclip.subclip(time_move[ first_clip_index],audio_duration-0.8)   \n",
    "    video_out= video_out.volumex(0)\n",
    "    video_out=video_out.set_audio(audioclip)\n",
    "    video_out.write_videofile(str(t)+\"版.MP4\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
